package com.webproject.shop.controller;


import com.webproject.shop.common.ErrorCode;
import com.webproject.shop.common.ResponseModel;
import com.webproject.shop.pojo.History;
import com.webproject.shop.pojo.User;
import com.webproject.shop.service.HistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.List;

@Controller
@RequestMapping("/history")
@CrossOrigin(origins = "${webproject.web.path}", allowedHeaders = "*", allowCredentials = "true")
public class HistoryController {

    @Autowired
    private HistoryService historyService;

    //获取历史记录
    @RequestMapping(path = "/historylist", method = RequestMethod.GET)
    @ResponseBody
    public ResponseModel getUserHistory(String username){
        List<History> histories = historyService.selectHistoryByUsername(username);
        return new ResponseModel(histories);
    }

    //添加历史记录
    @RequestMapping(path = "/addhistory", method = RequestMethod.POST)
    @ResponseBody
    public ResponseModel addHistory(History history){
        history.setTimeStamp(new Timestamp(System.currentTimeMillis()));
        historyService.insertHistory(history);
        return new ResponseModel(history);
    }

    //删除历史记录
    @RequestMapping(path = "/deletehistory", method = RequestMethod.POST)
    @ResponseBody
    public ResponseModel deleteHistory(HttpSession session,Integer id){
        History history=new History();
        User user = (User) session.getAttribute("loginUser");
        history.setUsername(user.getUsername());
        history.setProId(id);
        historyService.deleteHistory(history);
        return new ResponseModel(history);
    }


}
